/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.asu.cse360.service.impl;

import edu.asu.cse360.annotation.TargetEntity;
import edu.asu.cse360.dao.DataAccessObject;
import edu.asu.cse360.model.Restaurant;
import edu.asu.cse360.model.Table;
import edu.asu.cse360.model.TableStatus;
import edu.asu.cse360.service.TableService;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("tableService")
public class TableServiceImpl implements TableService{

    @Autowired
    @TargetEntity(Table.class)
    private DataAccessObject<Table, Integer> tableDao;
    
    @Override
    public List<Table> getTables(Restaurant restaurant) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("restaurant", restaurant);
        return tableDao.findList(Table.FIND_ALL_BY_RESTAURANT, params);
    }


    @Override
    public Table save(Table table) {
        if(table.getId() == null){
            tableDao.create(table);
        }else{
            tableDao.update(table);
        }
        return table;
    }

    @Override
    public Table getNextAvailable(Restaurant restaurant) {
        List<Table> tables = getTables(restaurant);
        for(Table table : tables){
            if(table.getStatus() == TableStatus.CLEANED){
                table.setStatus(TableStatus.OCCUPIED);
                this.save(table);
                return table;
            }
        }
        return null;
    }

}
